package com.xianyu.open.restclient.handler;


import com.xianyu.open.restclient.config.MethodInfo;
import com.xianyu.open.restclient.config.ServerInfo;
import com.xianyu.open.restclient.constants.RestClientGlobalConstants;
import com.xianyu.open.restclient.utils.RestTemplateUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author bailinan
 * @date 2018/6/18
 */
public class WebMvcRestClientHandler extends RestClientHandler {

    @Override
    @SuppressWarnings("unchecked")
    public <T> T invokeHandler(ServerInfo serverInfo, MethodInfo methodInfo, Class<T> resultType) {
        String url = getUrl(serverInfo, methodInfo);
        Object[] uriVariables = getUriVariables(url, methodInfo.getUriVariables());

        if (methodInfo.getResponseEntityType() != null){
            return (T) RestTemplateUtil.doRestForEntity(url, methodInfo.getHttpMethod(), getRequestParam(methodInfo),
                    methodInfo.getRequestHeader(),  methodInfo.getResponseEntityType(), uriVariables);
        }
        return RestTemplateUtil.doRest(url, methodInfo.getHttpMethod(), getRequestParam(methodInfo),
                methodInfo.getRequestHeader(),  resultType, uriVariables);
    }
}
